package 蓝桥算法;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;

public class BrickBreakerGame extends JFrame {
    // 游戏常量
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;
    private static final int PADDLE_WIDTH = 100;
    private static final int PADDLE_HEIGHT = 15;
    private static final int BALL_DIAMETER = 20;
    private static final int BRICK_WIDTH = 75;
    private static final int BRICK_HEIGHT = 30;
    private static final int DELAY = 10;

    // 游戏组件
    private Timer timer;
    private int paddleX;
    private int ballX, ballY;
    private int ballSpeedX = 3, ballSpeedY = -3;
    private boolean gameRunning = true;
    private ArrayList<Rectangle> bricks;

    public BrickBreakerGame() {
        // 初始化窗口
        setTitle("Java 打砖块游戏");
        setSize(WIDTH, HEIGHT);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // 初始化游戏组件
        paddleX = WIDTH / 2 - PADDLE_WIDTH / 2;
        ballX = WIDTH / 2;
        ballY = HEIGHT - 100;

        // 创建砖块
        createBricks();

        // 添加鼠标移动监听
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                paddleX = e.getX() - PADDLE_WIDTH / 2;
                if (paddleX < 0) paddleX = 0;
                if (paddleX > WIDTH - PADDLE_WIDTH) paddleX = WIDTH - PADDLE_WIDTH;
                repaint();
            }
        });

        // 游戏主循环
        timer = new Timer(DELAY, e -> {
            if (gameRunning) {
                moveBall();
                checkCollisions();
                repaint();
            }
        });
        timer.start();
    }

    private void createBricks() {
        bricks = new ArrayList<>();
        Random random = new Random();

        for (int row = 0; row < 4; row++) {
            for (int col = 0; col < 10; col++) {
                int brickX = col * (BRICK_WIDTH + 5) + 15;
                int brickY = row * (BRICK_HEIGHT + 5) + 50;
                Color color = new Color(
                        random.nextInt(200) + 55,
                        random.nextInt(200) + 55,
                        random.nextInt(200) + 55
                );
                bricks.add(new Rectangle(brickX, brickY, BRICK_WIDTH, BRICK_HEIGHT));
            }
        }
    }

    private void moveBall() {
        ballX += ballSpeedX;
        ballY += ballSpeedY;

        // 边界检测
        if (ballX <= 0 || ballX >= WIDTH - BALL_DIAMETER) {
            ballSpeedX = -ballSpeedX;
        }
        if (ballY <= 0) {
            ballSpeedY = -ballSpeedY;
        }

        // 游戏结束检测
        if (ballY >= HEIGHT) {
            gameRunning = false;
            timer.stop();
            JOptionPane.showMessageDialog(this, "游戏结束!", "提示", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }
    }

    private void checkCollisions() {
        // 检测与挡板的碰撞
        Rectangle paddleRect = new Rectangle(paddleX, HEIGHT - PADDLE_HEIGHT - 20, PADDLE_WIDTH, PADDLE_HEIGHT);
        Rectangle ballRect = new Rectangle(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);

        if (ballRect.intersects(paddleRect)) {
            ballSpeedY = -Math.abs(ballSpeedY);
            // 根据击打位置改变水平速度
            int hitPosition = (ballX + BALL_DIAMETER/2) - (paddleX + PADDLE_WIDTH/2);
            ballSpeedX = hitPosition / 10;
        }

        // 检测与砖块的碰撞
        for (int i = 0; i < bricks.size(); i++) {
            Rectangle brick = bricks.get(i);
            if (ballRect.intersects(brick)) {
                bricks.remove(i);
                ballSpeedY = -ballSpeedY;

                // 如果所有砖块都被消除
                if (bricks.isEmpty()) {
                    gameRunning = false;
                    timer.stop();
                    JOptionPane.showMessageDialog(this, "恭喜通关!", "提示", JOptionPane.INFORMATION_MESSAGE);
                    System.exit(0);
                }
                break;
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        // 清屏
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 绘制挡板
        g.setColor(Color.WHITE);
        g.fillRect(paddleX, HEIGHT - PADDLE_HEIGHT - 20, PADDLE_WIDTH, PADDLE_HEIGHT);

        // 绘制球
        g.setColor(Color.RED);
        g.fillOval(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);

        // 绘制砖块
        for (Rectangle brick : bricks) {
            g.setColor(brick.color);
            g.fillRect(brick.x, brick.y, brick.width, brick.height);
        }

        // 绘制游戏信息
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 16));
        g.drawString("剩余砖块: " + bricks.size(), 20, 30);
    }

    // 自定义矩形类，包含颜色属性
    private static class Rectangle extends java.awt.Rectangle {
        Color color;

        public Rectangle(int x, int y, int width, int height) {
            super(x, y, width, height);
            this.color = color;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            BrickBreakerGame game = new BrickBreakerGame();
            game.setVisible(true);
        });
    }
}